package com.lzkj.region.entity;

import lombok.Data;
import org.springframework.util.StringUtils;

/**
 * RawDataRegionDto
 *
 * @author raos
 * @email 991207823@qq.com
 * @date 2022/8/9 0009 9:40
 */
@Data
public class RawDataRegionDto {

    private String areaCode;
    private String province;
    private String city;
    private String county;
    private String town;
    private String village;
    private Integer type;

    /**
     * 获取实际插入对象
     *
     * @return
     */
    public SysRegionEntity gainSysRegionEntity() {
        SysRegionEntity entity = new SysRegionEntity();
        entity.setName(getName());
        entity.setType(getType());
        entity.setAreaCode(getAreaCode());
        return entity;
    }

    public String getAreaCode() {
        if (getType() == 1) {
            return areaCode.substring(0, 2);
        } else if (getType() == 2) {
            return areaCode.substring(0, 4);
        } else if (getType() == 3) {
            return areaCode.substring(0, 6);
        } else if (getType() == 4) {
            return areaCode.substring(0, 9);
        } else {
            return areaCode;
        }
    }

    /**
     * 获取行政地域名称
     *
     * @return
     */
    private String getName() {
        if (getType() == 1) {
            return province;
        } else if (getType() == 2) {
            return city;
        } else if (getType() == 3) {
            return county;
        } else if (getType() == 4) {
            return town;
        } else {
            return village;
        }
    }

    /**
     * 获取行政级别（0-5：国、省、市、县、镇、村）
     *
     * @return
     */
    public Integer getType() {
        if (!StringUtils.isEmpty(province) && !StringUtils.isEmpty(city) && !StringUtils.isEmpty(county)
                && !StringUtils.isEmpty(town) && !StringUtils.isEmpty(village)) {
            this.type = 5;
        } else if (!StringUtils.isEmpty(province) && !StringUtils.isEmpty(city) && !StringUtils.isEmpty(county)
                && !StringUtils.isEmpty(town)) {
            this.type = 4;
        } else if (!StringUtils.isEmpty(province) && !StringUtils.isEmpty(city) && !StringUtils.isEmpty(county)) {
            this.type = 3;
        } else if (!StringUtils.isEmpty(province) && !StringUtils.isEmpty(city)) {
            this.type = 2;
        } else {
            this.type = 1;
        }
        return this.type;
    }

}
